﻿namespace Shortcuts.Views
{
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using Shortcuts.Common;
    using Shortcuts.Globalization;
    using Shortcuts.ViewModels;
    using System;
    using System.Windows;
    using System.Windows.Navigation;

    public partial class Home : PhoneApplicationPage
    {
        HomePageContext Context { get { return DataContext as HomePageContext; } }

        #region buttons

        ApplicationBarIconButton[] buttons;

        void InitializeApplicationBarButtons()
        {
            if (buttons != null)
            {
                return;
            }

            buttons = new ApplicationBarIconButton[1];
            buttons[0] = new ApplicationBarIconButton();
            buttons[0].Text = Localized.Get("unpin");
            buttons[0].IconUri = new Uri("/Images/unpin.png", UriKind.Relative);
            buttons[0].Click += OnUnpinIconButtonClick;

            foreach (var button in buttons)
            {
                ApplicationBar.Buttons.Add(button);
            }

            ApplicationBarMenuItem menu = new ApplicationBarMenuItem();
            menu.Text = Localized.Get("about");
            menu.Click += OnAboutMenuClick;

            ApplicationBar.MenuItems.Add(menu);
        }

        #endregion

        public Home()
        {
            InitializeComponent();
            
            Loaded += OnPageLoaded;
        }

        void OnPageLoaded(object sender, RoutedEventArgs e)
        {
            InitializeApplicationBarButtons();
            DataContext = new HomePageContext();
        }

        private void OnUnpinIconButtonClick(object sender, EventArgs e)
        {
            foreach (var tile in TileHelper.Tiles)
            {
                tile.Delete();
            }
        }

        private void OnAboutMenuClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/AboutPage.xaml", UriKind.Relative));
        }
    }
}